package com.example.beryl.http;
import static android.content.ContentValues.TAG;
import android.util.Log;
import com.example.beryl.Util.HandlePublic;
import com.example.beryl.entity.Action;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * ；利用jar包提交Soap数据
 */
public class Net_method {
    private static String SOAP_ACTION ="http://tempuri.org/";//需要加上方法名
    private static String NAMESPACE ="http://tempuri.org/";//命名空间
    HandlePublic handlePublic = new HandlePublic();//对象不能在新线程下操作

    //private static String URL ="http://192.168.1.8:11000/WebService1.asmx";//服务器地址
    //private static String URL ="http://192.168.1.240/WebService1.asmx";//调试地址a
    //private static String IP = "http://192.168.1.240/";

    private static String IP = "http://172.168.1.253:9000/"; //服务器地址
    private static String IP1 = "http://192.168.10.100:10000/";//10.100

    //判断使用IP
    private String IPName()
    {
        String ret = IP;
        String MAC = HandlePublic.readLogFile(Action.IpGateWay,Action.IpGateWay);
        if (MAC != "")
        {
           if (MAC.equals("192.168.10.1"))
           {
               ret = IP1;
           }
        }
        return ret;
    }

    public ArrayList<String> SummitData(String METHOD_NAME, ArrayList<String> Parameters, ArrayList<String> ParValues,String URL)
    {
        ArrayList<String> list = new ArrayList<String>();
        try
        {
            String IpConn = "";
            String IpgateWay = HandlePublic.readLogFile(Action.IpGateWay,Action.IpGateWay);
            Boolean success;
            if (IpgateWay.equals("192.168.10.1"))
            {
                 success = HandlePublic.ping("192.168.10.100");
                 if (success)
                 {
                     IpConn = IP1;
                 }
            }else
            {
                success = HandlePublic.ping("172.168.1.253");
                if (success)
                {
                    IpConn = IP;
                }
            }
            if (!success)
            {
                //throw new Exception("1");
            }

            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
            //放入接口参数
            for (int i =0;i<Parameters.size();i++)
            {
                for (int j = 0;j < ParValues.size();j++)
                {
                    Request.addProperty(Parameters.get(i), ParValues.get(j));
                }
            }
            if (!(METHOD_NAME.equals("SelectAllCargoInfor") || METHOD_NAME.equals("HelloWorld")))
            {
                String rex = HandlePublic.readLogFile(Action.LoginJson,Action.LoginJson);
                if (rex != "")
                {
                   String[] data = rex.split(",");
                    HashMap<String,String> hashMap = new HashMap<>();
                    hashMap.put("账号",data[0]);
                    hashMap.put("MAC",data[1]);
                    Action.PhoneMessage = handlePublic.MakeJson(hashMap);
                }else
                {
                    throw new Exception("本地文件数据丢失，请重新打开程序");
                }
                Request.addProperty("Data",Action.PhoneMessage);
            }
            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(Request);

            HttpTransportSE transport= new HttpTransportSE(IpConn +URL);
            transport.call(SOAP_ACTION + METHOD_NAME, soapEnvelope);
            SoapObject resultString = (SoapObject) soapEnvelope.getResponse();
            //把返回结果放进list中
            for (int i = 0;i<resultString.getPropertyCount();i++)
            {
                list.add(resultString.getProperty(i).toString());
            }
            Log.i(TAG, "Result: " + resultString);
            return list;
        }
        catch(Exception ex)
        {
            Log.e(TAG, "Error: " + ex.getMessage());
            list.add("failed:"+ex.getMessage());
        }
        return list;
    }



    public ArrayList<String> SummitData1111(String METHOD_NAME, ArrayList<String> Parameters, ArrayList<String> ParValues,String URL)
    {
        ArrayList<String> list = new ArrayList<String>();
        try
        {
            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
            //放入接口参数
            for (int i =0;i<Parameters.size();i++)
            {
                for (int j = 0;j < ParValues.size();j++)
                {
                    Request.addProperty(Parameters.get(i), ParValues.get(j));
                }
            }
            if (!(METHOD_NAME.equals("SelectAllCargoInfor") || METHOD_NAME.equals("HelloWorld")))
            {
                String rex = HandlePublic.readLogFile(Action.LoginJson,Action.LoginJson);
                if (rex != "")
                {
                    String[] data = rex.split(",");
                    HashMap<String,String> hashMap = new HashMap<>();
                    hashMap.put("账号",data[0]);
                    hashMap.put("MAC",data[1]);
                    Action.PhoneMessage = handlePublic.MakeJson(hashMap);
                }else
                {
                    throw new Exception("本地文件数据丢失，请重新打开程序");
                }
                Request.addProperty("Data",Action.PhoneMessage);
            }
            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(Request);

            HttpTransportSE transport= new HttpTransportSE("http://192.168.1.240/" +URL);
            transport.call(SOAP_ACTION + METHOD_NAME, soapEnvelope);
            SoapObject resultString = (SoapObject) soapEnvelope.getResponse();
            //把返回结果放进list中
            for (int i = 0;i<resultString.getPropertyCount();i++)
            {
                list.add(resultString.getProperty(i).toString());
            }
            Log.i(TAG, "Result: " + resultString);
            return list;
        }
        catch(Exception ex)
        {
            Log.e(TAG, "Error: " + ex.getMessage());
            list.add("failed:"+ex.getMessage());
        }
        return list;
    }


    //数据库数据返回String
    public String SummitData1(String METHOD_NAME,String Parameters,String ParValues,String URL)
    {
        ArrayList<String> list = new ArrayList<String>();
        String ret = "";
        try
        {
            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
            //放入接口参数
            Request.addProperty(Parameters, ParValues);
            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(Request);
            String IPName = IP;

            HttpTransportSE transport= new HttpTransportSE(IPName()+ "http://192.168.1.240/");
            transport.call(SOAP_ACTION + METHOD_NAME, soapEnvelope);
            SoapObject resultString = (SoapObject) soapEnvelope.getResponse();
            if (resultString.getPropertyCount() > 1)
            {
                throw new Exception("返回结果长度超出，请联系管理员!");
            }else
            {
                ret = resultString.getProperty(0).toString();
            }
//            //把返回结果放进list中
//            for (int i = 0;i<resultString.getPropertyCount();i++)
//            {
//                list.add(resultString.getProperty(i).toString());
//            }
            Log.i(TAG, "Result: " + ret);
            return ret;
        }
        catch(Exception ex)
        {
            Log.e(TAG, "Error: " + ex.getMessage());
           // list.add("failed:"+ex.getMessage());
            return ex.getMessage();
        }
    }



//    public ArrayList<String> SummitData(String METHOD_NAME, ArrayList<String> Parameters, ArrayList<String> ParValues)
//    {
//        ArrayList<String> list = new ArrayList<String>();
//        try
//        {
//            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
//            //放入接口参数
//            for (int i =0;i<Parameters.size();i++)
//            {
//                for (int j = 0;j < ParValues.size();j++)
//                {
//                    Request.addProperty(Parameters.get(i), ParValues.get(j));
//                }
//            }
//            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//            soapEnvelope.dotNet = true;
//            soapEnvelope.setOutputSoapObject(Request);
//            HttpTransportSE transport= new HttpTransportSE(URL);
//            transport.call(SOAP_ACTION + METHOD_NAME, soapEnvelope);
//            SoapObject resultString = (SoapObject) soapEnvelope.getResponse();
//            //把返回结果放进list中
//            for (int i = 0;i<resultString.getPropertyCount();i++)
//            {
//                list.add(resultString.getProperty(i).toString());
//            }
//            Log.i(TAG, "Result: " + resultString);
//            return list;
//        }
//        catch(Exception ex)
//        {
//            Log.e(TAG, "Error: " + ex.getMessage());
//            list.add("failed:"+ex.getMessage());
//        }
//        return list;
//    }




  //普通网络提交方法

//    public ArrayList<String> GetWebServre(String methodName, ArrayList<String> Parameters, ArrayList<String> ParValues) {
//        ArrayList<String> Values = new ArrayList<String>();
//        //ServerUrl是指webservice的url
//        //10.0.2.2是让android模拟器访问本地（PC）服务器，不能写成127.0.0.1
//        //11125是指端口号，即挂载到IIS上的时候开启的端口
//        //Service1.asmx是指提供服务的页面
//        String ServerUrl = "http://192.168.1.240/WebService1.asmx";
//        //String soapAction="http://tempuri.org/LongUserId1";
//        String soapAction = "http://tempuri.org/" + methodName;
//        //String data = "";
//        String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
//                + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
//                + "<soap:Body />";
//        String tps, vps, ts;
//        String mreakString = "";
//
//        mreakString = "<" + methodName + " xmlns=\"http://tempuri.org/\">";
//        for (int i = 0; i < Parameters.size(); i++) {
//            tps = Parameters.get(i).toString();
//            //设置该方法的参数为.net webService中的参数名称
//            vps = ParValues.get(i).toString();
//            ts = "<" + tps + ">" + vps + "</" + tps + ">";
//            mreakString = mreakString + ts;
//        }
//        mreakString = mreakString + "</" + methodName + ">";
//		/*
//		+"<HelloWorld xmlns=\"http://tempuri.org/\">"
//		+"<x>string11661</x>"
//		+"<SF1>string111</SF1>"
//		+ "</HelloWorld>"
//		*/
//        String soap2 = "</soap:Envelope>";
//        String requestData = soap + mreakString + soap2;
//        //System.out.println(requestData);
//
//        try {
//            URL url = new URL(ServerUrl);
//            HttpURLConnection con = (HttpURLConnection) url.openConnection();
//            byte[] bytes = requestData.getBytes("utf-8");
//            con.setDoInput(true);
//            con.setDoOutput(true);
//            con.setUseCaches(false);
//            con.setConnectTimeout(6000);// 设置超时时间
//            con.setRequestMethod("POST");
//            con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
//            con.setRequestProperty("SOAPAction", soapAction);
//            con.setRequestProperty("Content-Length", "" + bytes.length);
//            OutputStream outStream = con.getOutputStream();
//            outStream.write(bytes);
//            outStream.flush();
//            outStream.close();
//            InputStream inStream = con.getInputStream();
//            //data=parser(inStream);
//            //System.out.print("11");
//            Values = inputStreamtovaluelist(inStream, methodName);
//            //System.out.println(Values.size());
//            return Values;
//
//        } catch (Exception e) {
//            System.out.print("2221");
//            return null;
//        }
//    }
//    public ArrayList<String> inputStreamtovaluelist(InputStream in, String MonthsName) throws IOException {
//        StringBuffer out = new StringBuffer();
//        String s1 = "";
//        byte[] b = new byte[4096];
//        ArrayList<String> Values = new ArrayList<String>();
//        Values.clear();
//
//        for (int n; (n = in.read(b)) != -1;) {
//            s1 = new String(b, 0, n);
//            out.append(s1);
//        }
//
//        System.out.println(out);
//        String[] s13 = s1.split("><");
//        String ifString = MonthsName + "Result";
//        String TS = "";
//        String vs = "";
//
//        Boolean getValueBoolean = false;
//        for (int i = 0; i < s13.length; i++) {
//            TS = s13[i];
//            System.out.println(TS);
//            int j, k, l;
//            j = TS.indexOf(ifString);
//            k = TS.lastIndexOf(ifString);
//
//            if (j >= 0) {
//                System.out.println(j);
//                if (getValueBoolean == false) {
//                    getValueBoolean = true;
//                } else {
//
//                }
//                if ((j >= 0) && (k > j)) {
//                    System.out.println("FFF" + TS.lastIndexOf("/" + ifString));
//                    //System.out.println(TS);
//                    l = ifString.length() + 1;
//                    vs = TS.substring(j + l, k - 2);
//                    //System.out.println("fff"+vs);
//                    Values.add(vs);
//                    System.out.println("退出" + vs);
//                    getValueBoolean = false;
//                    return Values;
//                }
//            }
//            if (TS.lastIndexOf("/" + ifString) >= 0) {
//                getValueBoolean = false;
//                return Values;
//            }
//            if ((getValueBoolean) && (TS.lastIndexOf("/" + ifString) < 0) && (j < 0)) {
//                k = TS.length();
//
//                if (k == 8)//当数据库返回null时 赋值vs为1
//                {
//                    vs = "1";
//                }
//                else
//                {
//                    vs = TS.substring(7, k - 8);
//                }
//                //System.out.println(TS);
//
//                //System.out.println("f"+vs);
//                Values.add(vs);
//            }
//        }
//        return Values;
//    }
}
